<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CORS跨域资源共享</title>
</head>
<body>
    
    <script src="../node_modules/axios/dist/axios.js"></script>
    <script>
        axios.defaults.baseURL = 'http://127.0.0.1:8000';
        axios.defaults.withCredentials = true;
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        axios.defaults.transformRequest = function(data){
            if(!data) return;
            let result = "";
            for(let attr in data){
                if(!data.hasOwnProperty(attr)) break;
                result += `&${attr}=${data[attr]}`;
            }
            return result.substring(1);
        }
        axios.interceptors.response.use(function onFulfilled(response){
            return response.data;
        }, function onReject(reason){
            return Promise.reject(reason);
        });
        axios.defaults.validateStatus = function(status){
            return /^(2|3)\d{2}$/.test(status);
        }
        axios.get('http://127.0.0.1:8000/cors')
            .then(result => {
                console.log(result)
            })
    </script>
</body>
</html>